<template>
  <div class="containers">
    <el-card class="box-card">
      <el-row>
        <el-col :span="18">
          <el-form :inline="true" :model="params" class="demo-form-inline">
            <el-form-item label="关键字">
              <el-input
                class="a"
                v-model="params.keyword"
                placeholder="根据文章标题搜索"
              ></el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="params.state"  placeholder="请选择">
                <el-option v-for="item in topState" :key="item.id" :label="item.value" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <div class="rigth_form">
                <el-button class="btn" size="small" @click="clearBtn"
                  >清除</el-button
                >
                <el-button class="btn" type="primary" size="small" @click="searchBtn"
                  >搜索</el-button
                >
              </div>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6">
          <el-button
            type="success"
            size="small"
            icon="el-icon-edit"
            @click="addArticle"
            >新增技巧</el-button
          >
        </el-col>
      </el-row>
      <el-alert
        class="msg"
        :title="`数据一共${counts}条`"
        type="info"
        show-icon
      >
      </el-alert>
      <el-table :data="list" style="width: 100%">
        <el-table-column
          label="序号"
          type="index"
          width="80"
          :index="indexMethod(1)"
        >
        </el-table-column>
        <el-table-column prop="title" label="文章标题" width="400">
          <template #default="scope">
            {{scope.row.title}} <span @click="showVideo(scope.row.videoURL)" :class="scope.row.videoURL?'el-icon-film':''"></span>
          </template>
        </el-table-column>
        <el-table-column prop="visits" label="阅读数"> </el-table-column>
        <el-table-column prop="username" label="录入人"> </el-table-column>
        <el-table-column prop="createTime" label="录入时间">
          <template #default="scope">
            {{ formatDate(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="state" label="状态" width="100">
          <template #default="scope">
            {{ formOfEmployment(scope.row.state) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button type="text" size="medium" @click="showdialog(scope.row)"
              >预览</el-button
            >
            <el-button
              type="text"
              size="medium"
              @click="stateBtn(scope.row, scope.row.state)"
            >
              {{ scope.row.state === 1 ? "禁用" : "启用" }}
              <!-- {{state?'禁用':'启用'}} -->
            </el-button>
            <el-button
              type="text"
              size="medium"
              :disabled="scope.row.state === 1"
              @click="updArticle(scope.row.id)"
              >修改</el-button
            >
            <el-button
              type="text"
              size="medium"
              :disabled="scope.row.state === 1"
              @click="delArticle(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="page"
        background
        :page-sizes="[5, 10, 20]"
        layout=" prev,pager, next,sizes, jumper"
        :total="counts"
        :page-size="10"
        @current-change="pageChange"
        @size-change="pageSize"
      >
      </el-pagination>
    </el-card>
    <articlesPreview
      :show-dialog="showDialog"
      @close-dialog="closeDialog"
      :article-view="articleView"
    />
    <arricleAdd
      :show-article-dialog="showArticleDialog"
      @close-dialog="closeDialog"
      @load-list="loadList"
      :curId="curId"
    />
    <div class="video-preview" v-if="show">
      <div class="close">
        <span @click="viewClose" class="el-icon-error"></span>
      </div>
      <div class="box">
        <video class="video" :src="url" autoplay controls></video>
      </div>
    </div>
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/articles'
import dayjs from 'dayjs'
import articlesPreview from '@/module-hmmm/components/articles-preview'
import arricleAdd from '@/module-hmmm/components/articles-add'
export default {
  data () {
    return {
      params: {
        page: 1,
        pagesize: 10,
        keyword: '',
        state: null
      },
      list: [],
      counts: 0, // 总数据
      showDialog: false,
      articleView: {},
      showArticleDialog: false,
      curId: 0,
      topState: [
        { id: 1, value: '启用' },
        { id: 0, value: '禁用' }
      ],
      show: false,
      url: ''
    }
  },
  components: {
    articlesPreview,
    arricleAdd
  },
  mounted () {
    this.loadList()
  },
  methods: {
    // 清除按钮
    clearBtn () {
      this.params.keyword = ''
      this.params.state = null
      this.loadList()
    },
    // 获取列表信息
    async loadList () {
      const res = await list(this.params)
      console.log(res.data.items)
      this.list = res.data.items
      this.counts = res.data.counts
    },
    // 时间格式化
    formatDate (value, str = 'YYYY-MM-DD HH:mm:ss') {
      return dayjs(value).format(str)
    },
    // 状态转换
    formOfEmployment (type) {
      const map = {
        1: '已启用',
        0: '已禁用'
      }
      return map[type]
    },
    // 分页
    pageChange (page) {
      this.params.page = page
      this.loadList()
    },
    // 序号
    indexMethod (index) {
      if (this.params.page !== 1) {
        return (this.params.page - 1) * this.params.pagesize + 1
      } else {
        return index++
      }
    },
    // 每页几条
    pageSize (size) {
      this.params.pagesize = size
      this.loadList()
    },
    // 点击搜索按钮
    searchBtn () {
      this.loadList()
      console.log(this.list)
    },
    // 状态请求
    async loadChangeState (id, state) {
      await changeState({ id, state })
      this.loadList()
    },
    // 启用禁用按钮
    stateBtn (row, state) {
      if (state === 1) {
        row.state = 0
        this.loadChangeState(row.id, row.state)
        this.$message({
          message: '禁用成功!',
          type: 'success'
        })
      } else {
        row.state = 1
        this.loadChangeState(row.id, row.state)
        this.$message({
          message: '启用成功!',
          type: 'success'
        })
      }
    },
    // 关闭预览
    closeDialog () {
      this.showDialog = false
      this.showArticleDialog = false
      this.curId = 0
    },
    // 打开预览弹窗
    showdialog (row) {
      this.showDialog = true
      this.articleView = row
      console.log(row)
    },
    // 打开新增弹窗
    addArticle () {
      this.showArticleDialog = true
    },
    // 打开修改弹窗
    updArticle (id) {
      this.showArticleDialog = true
      this.curId = id
    },
    // 点击删除按钮
    delArticle (id) {
      this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await remove({ id })
        console.log(res)
        this.loadList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
      })
    },
    // 打开视频
    showVideo (url) {
      this.show = true
      this.url = url
    },
    closeVideo () {
      this.show = false
    },
    viewClose () {
      this.closeVideo()
    }
  }
}
</script>

<style scoped lang='scss'>
 .containers {
  padding: 10px;
}
.el-card__body {
  padding: 20px;
}
::v-deep .demo-form-inline .el-form-item__label {
  width: 80px;
  line-height: 32px;
}
::v-deep .el-form-item--medium {
  margin-bottom: 18px;
}
::v-deep .el-icon-arrow-up {
  line-height: 34px;
}
::v-deep .el-form-item__content .el-input__inner {
  height: 32px;
  line-height: 32px;
  font-size: 13px;
  vertical-align: top;
}
.a {
  width: 188px;
  height: 32px;
  vertical-align: top;
}
::v-deep .el-input--suffix {
  width: 203px;
  height: 32px;
  vertical-align: top;
}
::v-deep .el-col-6 {
  text-align: right;
}
.btn {
  vertical-align: top;
}
.msg {
  margin-bottom: 15px;
}
.page {
  margin-top: 20px;
  text-align: right;
}
::v-deep .is-in-pagination .el-input__inner {
  height: 28px;
}
.el-icon-film{
  font-size: 18px;
  color: blue;
}
.video-preview{
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,.3);
  overflow: hidden;
  z-index: 1000;
  .close{
    width: 50px;
    height: 50px;
    position: absolute;
    top: 30px;
    left: 50%;
    transform: translate(-50%);
    background: rgba(0,0,0,.4);
    box-shadow: 0 0 5px rgb(0 0 0 / 40%);
    border-radius: 50%;
    text-align: center;
    line-height: 50px;
    color: #fff;
    font-size: 20px;
    z-index: 1100;
    .el-icon-error{
      width: 100%;
      height: 100%;
    }
  }
  .box{
    width: 800px;
    height: 450px;
    position: absolute;
    left: 50%;
    top:50%;
    transform: translate(-50%,-50%);
    z-index: 1010;
    .video{
      width: 100%;
      height: 100%;
    }
  }
}
</style>
